package com.smartcat.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.log4j.Logger;


public class ToolsKitUtil {
	
	private static final SimpleDateFormat dateFormat=new SimpleDateFormat("yyMMddHHmmss");
	private static SimpleDateFormat dateFormatShow=new SimpleDateFormat("yyyy-MM-dd");

	public static String getNewTimeString(){
		return dateFormat.format(Calendar.getInstance(TimeZone.getTimeZone("GMT+8")).getTime());
	}
	
	public static Date transStringToDate(String timeStr) throws Exception{
		return dateFormat.parse(timeStr);
	}
	
	public static String getOldTimeString(int days){
		Calendar c=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
		c.add(Calendar.DAY_OF_MONTH, days);
		Date date=c.getTime();
		return dateFormat.format(date);
		
	}
	
	public static Date getDateBefore(int days){
		Calendar c=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
		c.add(Calendar.DAY_OF_MONTH, days);
		Date date=c.getTime();
		return date;
		
	}
	
	public static void logInfo(Class cl,String logStr){
		Logger logger=Logger.getLogger(cl);
		//System.out.println("ToolsKitUtil------"+logStr);
		logger.info(logStr);
	}
	
	public static void logError(Class cl,String logStr){
		Logger logger=Logger.getLogger(cl);
		logger.error(logStr);
	}
	
	public static String encode(String str){
		String encoded="";
		try {
			encoded = URLEncoder.encode(str,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return encoded;
	}
	
	public static String decode(String str){
		String decoded="";
		
		try {
			decoded=URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return decoded;
	}
	
	
	public static String formatDate(Date date){
		return dateFormatShow.format(date);
	}
	
}
